/****************************************************************************
**
** Copyright (C) 2018 Alexander Akulich <akulichalexander@gmail.com>
**
** Sailfish Silica UI is under copyright of Jolla Ltd.
**
** Based on Qt Material style
**
****************************************************************************/

#include "qquicksilicatheme_p.h"

#include <QtGui/qpa/qplatformdialoghelper.h>
#include <QtGui/qfont.h>
#include <QtGui/qfontinfo.h>

QT_BEGIN_NAMESPACE

QQuickSilicaTheme::QQuickSilicaTheme(QPlatformTheme *theme)
    : QQuickProxyTheme(theme)
{
    QFont font;
    font.setFamily(QLatin1String("Roboto"));
    QString family = QFontInfo(font).family();

    if (family != QLatin1String("Roboto")) {
        font.setFamily(QLatin1String("Noto"));
        family = QFontInfo(font).family();
    }

    if (family == QLatin1String("Roboto") || family == QLatin1String("Noto")) {
        systemFont.setFamily(family);
        buttonFont.setFamily(family);
        toolTipFont.setFamily(family);
        itemViewFont.setFamily(family);
        listViewFont.setFamily(family);
        menuItemFont.setFamily(family);
        editorFont.setFamily(family);
    }

    systemFont.setPixelSize(14);

    buttonFont.setPixelSize(14);
    buttonFont.setCapitalization(QFont::AllUppercase);
    buttonFont.setWeight(QFont::Medium);

    toolTipFont.setPixelSize(14);
    toolTipFont.setWeight(QFont::Medium);

    itemViewFont.setPixelSize(14);
    itemViewFont.setWeight(QFont::Medium);

    listViewFont.setPixelSize(16);

    menuItemFont.setPixelSize(16);

    editorFont.setPixelSize(16);
}

const QFont *QQuickSilicaTheme::font(QPlatformTheme::Font type) const
{
    switch (type) {
    case QPlatformTheme::TabButtonFont:
    case QPlatformTheme::PushButtonFont:
    case QPlatformTheme::ToolButtonFont:
        return &buttonFont;
    case QPlatformTheme::TipLabelFont:
        return &toolTipFont;
    case QPlatformTheme::ItemViewFont:
        return &itemViewFont;
    case QPlatformTheme::ListViewFont:
        return &listViewFont;
    case QPlatformTheme::MenuItemFont:
    case QPlatformTheme::ComboMenuItemFont:
        return &menuItemFont;
    case QPlatformTheme::EditorFont:
        return &editorFont;
    default:
        return &systemFont;
    }
}

QVariant QQuickSilicaTheme::themeHint(ThemeHint hint) const
{
    switch (hint) {
    case QPlatformTheme::DialogButtonBoxLayout:
        // https://material.io/guidelines/components/dialogs.html#dialogs-specs
        // As per spec, affirmative actions are placed to the right, dismissive
        // actions are placed directly to the left of affirmative actions.
        // In the Android sources, there are additional type of actions -
        // neutral, which are placed to the left.
        // Rules for macOS seems to be the most suitable here and are also used
        // in the Android QPA plugin.
        return QVariant(QPlatformDialogHelper::MacLayout);
    default:
        return QQuickProxyTheme::themeHint(hint);
    }
}

QT_END_NAMESPACE
